<template>
  <div class="user-profile">
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <input type="file" ref="file" hidden @change="onFileChange" />
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image class="avater" round fit="cover" :src="userList.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="userList.name"
      is-link
      @click="isUserNameEditShow = true"
    />
    <van-cell
      @click="isUserGenderEditShow = true"
      title="性别"
      :value="userList.gender === 0 ? '男' : '女'"
      is-link
    />
    <van-cell
      title="生日"
      :value="userList.birthday"
      is-link
      @click="isUserBirthdayEditShow = true"
    />
    <van-popup
      v-model="isUserNameEditShow"
      style="height: 100%"
      position="bottom"
    >
      <edit-name
        v-if="isUserNameEditShow"
        @close="isUserNameEditShow = false"
        v-model="userList.name"
      />
    </van-popup>
    <van-popup v-model="isUserGenderEditShow" position="bottom">
      <edit-gender
        v-if="isUserGenderEditShow"
        @close="isUserGenderEditShow = false"
        v-model="userList.gender"
      />
    </van-popup>
    <van-popup v-model="isUserBirthdayEditShow" position="bottom">
      <edit-birthday
        v-if="isUserBirthdayEditShow"
        @close="isUserBirthdayEditShow = false"
        v-model="userList.birthday"
      />
    </van-popup>
    <van-popup
      v-model="isUserPhotoEditShow"
      position="bottom"
      style="height: 100%"
    >
      <edit-photo
        v-if="isUserPhotoEditShow"
        :img="img"
        @close="isUserPhotoEditShow = false"
        @edit-photo="userList.photo = $event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUsers } from '@/api/users'
import EditName from './components/edit-name'
import EditGender from './components/edit-gender'
import EditBirthday from './components/edit-birthday.vue'
import EditPhoto from './components/edit-photo.vue'
export default {
  neme: 'CloseReply',
  components: {
    EditName,
    EditGender,
    EditBirthday,
    EditPhoto
  },
  data () {
    return {
      userList: [],
      isUserNameEditShow: false,
      isUserGenderEditShow: false,
      isUserBirthdayEditShow: false,
      isUserPhotoEditShow: false,
      img: null
    }
  },
  created () {
    this.loadUsers()
  },
  methods: {
    async loadUsers () {
      try {
        const { data } = await getUsers()
        console.log(data, '用户资料')
        this.userList = data.data
      } catch (err) {
        this.$toast('获取用户资料失败')
      }
    },
    onFileChange () {
      const file = this.$refs.file.files[0]
      this.img = window.URL.createObjectURL(file)
      this.isUserPhotoEditShow = true
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  .page-nav-bar {
    background-color: rgb(49, 74, 216);
  }
  .avater {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f6f7fa;
  }
}
</style>
